<?php defined('SYSPATH') OR die('No direct access allowed.');

// Dies ist der Basis-Controller er Applikation. 
class Viewbase_Controller extends Base_Controller{
	
	 
	public $orderby=array('position'=>'ASC');
	public $forms=array(array('title'=>'Input', 'link'=>'input'), array('title'=>'Action', 'link'=>'action'));
	public $parent_column='parent_id';


	public function __construct()
	{ 	  	
		parent::__construct();	
		if (!$this->auth->logged_in()){
	    	url::redirect('login');
        }else{
	   		$this->user = $this->auth->get_user(); //now you have access to user information stored in the database
		}				
	}  
	
	public function __get_model($id=NULL){
		if($this->modelname){
			$model=ORM::factory($this->modelname, $id);
			return $model;
		}
		return NULL;	
	}


	public function single($id, $id2=NULL){
		$this->__setlastview();
		$model=$this->__get_model($id);
		
		$view=new View($this->controllername.'/single');
		$view->model=$model;
		$view->id2=$id2;
				
		$this->template->content=$view;
		if($model->has_property('title')){
			$this->template->title=$model->title;	
		}else{
			$this->template->title=$this->controllername;	
		}
	}
	
	public function listall($id=NULL, $id2=NULL, $id3=NULL){
		$this->__setlastview();
		$model=$this->__get_model();
		$view=new View($this->controllername.'/listall');
		$view->model=$model;
		$view->id=$id;
		$view->id2=$id2;
		$view->id3=$id3;
		$this->template->content=$view;		
		$this->template->title=$this->controllername;
	}
	
	public function staticpage($id=NULL, $id2=NULL, $id3=NULL){
		$this->__setlastview();
		$view=new View($this->controllername.'/'.$id);
		$view->id=$id2;
		$view->id2=$id3;
		$this->template->content=$view;
		$this->template->title=$this->controllername;		
	}
	
	
	
	public function __setlastview($doset=TRUE){
		
		if($doset){
			$this-> session->set('lastview', $this->uri->string());
			$this->session->set('lastview_array', $this->uri->segment_array());
		}
		parent::__initmenu();
	}
	
	public function form($type='input', $id=NULL){
	
		$model=ORM::factory($this->modelname, $id);	
		$this->__setlastview(FALSE);
		
		if(isset($_POST['close'])){	
			url::redirect($this->session->get('lastview'));	
		}
		
		if(isset($_POST['save'])){		
			$model=$this->__save_post($model);
			url::redirect(CONTDIR.$this->controllername.'/form/'.$type.'/'.$model->id);
		}
		
		if(isset($_POST['saveandclose'])){	
			$model=$this->__save_post($model);
			url::redirect($this->session->get('lastview'));	
		}
		
		$formheader=new View('formheader');
		$formheader->controller=$this->controllername;
		$formheader->selected=$type;
		$formheader->id=$id;
		$formheader->params=$this->forms;
		
		$view=new View($this->controllername.'/form/'.$type);
		$view->model=$model;	
		$view->type=$type;
		$view->formheader=$formheader;
		
		$hidden=array();
		$view->hidden=$hidden;
				
		$this->template->content=$view;		
	}
	
	public function action($type, $id=NULL, $param=NULL){
		if(method_exists($this, '__'.$type )){
			
			if($param!=NULL){
				$this->{'__'.$type}($id,$param);
			}else{
				$this->{'__'.$type}($id);
			}
		}	
		url::redirect($this->session->get('lastview'));
	}
	
	
	
	
	public function __save_post($model){
	
		$model=$this->__preprocess_post($model);						
		foreach($this->input->post() as $key=>$value){
			if($model->has_property($key)){
				$model->$key=$value;
			}	
		}
		$model->save();
		$this->__postprocess_model($model);
		$model->save();	
		$this->__rewrite_positions($model);
		return $model;
	}
		
	public function __preprocess_post($model){	
		return $model;
	}
	
	
	public function __postprocess_model($model){
		
	}	
	
	public function __post_to_model($model){
		foreach($this->input->post() as $key=>$value){
			if($model->has_property($key)){
				$model->$key=$value;
			}	
		}
		return $model;
	}
	
	public function get_parent_column_name($model=NULL){
		return $this->parent_column;
	}


	


/////////////////////////////////////////// Actions
	
	public function __delete($id){
		$model=ORM::factory($this->modelname, $id);
		$this->__last_words($model);			
		$pc=$this->get_parent_column_name($model);
		if($pc!=''){
			$pid=$model->$pc;
		}
		$model->delete();
		if($pc!=''){
			$this->__rewrite_positions2($pc, $pid);
		}
	}
	
	public function __last_words($model){
		
	}
	
	public function __up($id){
		$model=ORM::factory($this->modelname, $id);
		$model->position-=15;
		$model->save();
		
		$this->__rewrite_positions($model);
		
	}
	
	public function __down( $id){
		$model=ORM::factory($this->modelname, $id);
		$model->position+=15;
		$model->save();
		$this->__rewrite_positions($model);
	}
	
	
	public function __rewrite_positions($model){	
		$pc=$this->get_parent_column_name($model);
		if($pc=='')
			return;
			
		$pid=$model->$pc;
		$models=ORM::factory($this->modelname)->where($pc,$pid)->orderby($this->orderby)->find_all();
		$this->__rewrite_them($models);
		
		
	}

	public function __rewrite_positions2($pc, $pid){
		$models=ORM::factory($this->modelname)->where($pc,$pid)->orderby($this->orderby)->find_all();
		$this->__rewrite_them($models);
		
	}
	
	public function __rewrite_them($models){
		$pos=0;	
		foreach($models as $model){
			$model->position=$pos;
			$model->save();
			$pos+=10;		
		}
	}
	
	public function __move_to( $id ){
		$model=ORM::factory($this->modelname, $id);
		$pc=$this->get_parent_column_name($model);
		$pid1=$model->$pc;
		$pid2=$this->input->post('new_parent_id');
		$model->$pc=$pid2;
		$model->position=1000;
		$model->save();	
		$this->__rewrite_positions2($pc, $pid1);
		$this->__rewrite_positions2($pc, $pid2);
	}
	
	public function __copy_to($id){
		
		$modelold=ORM::factory($this->modelname,$id)->as_array();
		$modelnew=ORM::factory($this->modelname);
		
		foreach($modelold as $key=>$param){
			if($key!='id'){
				$modelnew->$key=$param;
			}
		}
		if($this->input->post('new_title')){
			$modelnew->title=$this->input->post('new_title');
		}
		
		if($this->input->post('new_page_id')){
			$modelnew->page_id=$this->input->post('new_page_id');
		}
		
		
		$modelnew->save();
		$this->__rewrite_positions($modelnew);
	}
	
	
	public function __empty($id, $param){
		$model=ORM::factory($this->modelname,$id);
		foreach($model->$param as $elm){
			$elm->delete();
		}	
	}
	
	public function __removefiles($id){
		$dir=$this->input->post('directory');
		$files = glob($dir.'*');
		array_map('unlink', $files);  		
	}
	
	
	
	///////////////////////////////////////////   AJAX CALLS
	
	public function rewrite_positions($parentid){
	 	if( request::is_ajax()){
			$pos=0;
			foreach($this->input->post("data") as $elmid){
				$model=ORM::factory($this->modelname, $elmid);
				$model->position=$pos;
				$pos+=10;
				$model->save();
			}
			die();
		}
		
	}
	

}
